home *** CD-ROM | disk | FTP | other *** search
- unit BitManip;
- // unit for bit manipulation
- ////////////////////////////////////////////////////////////////////////////////
- interface
- uses Classes;
- type TFakeBits = class
- public
- FSize: Integer;
- FBits: Pointer;
- end;
- type tEnhancedBits = class(tBits)
- ffk : tFakeBits;
- constructor Create;
- procedure RemoveBit(idx:integer);
- procedure InsertBit(idx:integer;b:boolean);
- end;
- ////////////////////////////////////////////////////////////////////////////////
- implementation
- uses Consts;
- const BitsPerInt = SizeOf(Integer) * 8;
-
- constructor tEnhancedBits.Create;
- begin
- inherited Create;
- ffk:=tFakeBits(self);
- end;
-
- procedure tEnhancedBits.RemoveBit(idx:integer);
- var p,j,i : integer;
- tmp : tEnhancedBits;
- begin
- // check index
- if (idx<0) or (idx>=size) then
- raise EBitsError.CreateRes(SBitsIndexError);
- // create temporary class
- tmp:=tEnhancedBits.create;
- // set size of temporary class
- tmp.size:=size-1;
- // load temporary class
- j:=0;
- for i:=0 to size-1 do if (i<>idx) then
- begin
- tmp[j]:=bits[i];
- inc(j);
- end;
- // deallocate bit array of old class
- Size:=0;
- // set new size
- Size:=tmp.Size;
- // compute size of allocated bit array
- p:=((tmp.Size + BitsPerInt - 1) div BitsPerInt)*sizeof(integer);
- // move new array
- move(tmp.ffk.FBits^,ffk.FBits^,p);
- // free temporary class
- tmp.free;
- end;
-
- procedure tEnhancedBits.InsertBit(idx:integer;b:boolean);
- var p,j,i : integer;
- tmp : tEnhancedBits;
- begin
- if (idx<0) or (idx>size) then
- raise EBitsError.CreateRes(SBitsIndexError);
- // create temporary class
- tmp:=tEnhancedBits.create;
- // set size of temporary class
- tmp.size:=size+1;
- // load temporary class
- j:=0;
- for i:=0 to size-1 do
- begin
- if i=idx then
- begin
- tmp[j]:=b;
- inc(j);
- end;
- tmp[j]:=bits[i];
- inc(j);
- end;
- if idx=size then tmp[j]:=b;
- // deallocate bit array of old class
- Size:=0;
- // set new size
- Size:=tmp.Size;
- // compute size of allocated bit array
- p:=((tmp.Size + BitsPerInt - 1) div BitsPerInt)*sizeof(integer);
- // move new array
- move(tmp.ffk.FBits^,ffk.FBits^,p);
- // free temporary class
- tmp.free;
- end;
- ////////////////////////////////////////////////////////////////////////////////
- end.
-